package com.kakaxi.mybilibili.module.home.live.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.module.home.live.model.ILiveRoomModel;
import com.kakaxi.mybilibili.module.home.live.model.LiveRoomModel;
import com.kakaxi.mybilibili.module.home.live.view.ILiveRoomView;
import com.kakaxi.mybilibili.bean.LiveRoomBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

/**
 * 直播间控制层实现类
 * Created by kakaxi on 16/11/4.
 */

public class LiveRoomPresenter implements ILiveRoomPresenter {

    /**
     * 直播视图层
     */
    private ILiveRoomView mLiveRoomView;

    /**
     * 直播数据层
     */
    private ILiveRoomModel mLiveRoomModel;

    public LiveRoomPresenter(ILiveRoomView liveRoomView) {
        mLiveRoomView = liveRoomView;
        mLiveRoomModel = new LiveRoomModel();
    }

    @Override
    public void initLiveRoomData(int roomId) {
        mLiveRoomModel.getLiveRoomData(roomId, new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                // 判断是否正确
                if (response.getString(WebParamConst.RESPONSE_CODE).equals
                        (WebParamConst.RESULT_CODE_OK)) {
                    // 把数据转成直播间信息类
                    LiveRoomBean liveRoomBean = JSON.parseObject(response.getJSONObject
                            (WebParamConst.RESPONSE_DATA).toJSONString(), LiveRoomBean.class);
                    mLiveRoomView.showLiveRoomData(liveRoomBean);
                } else {

                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
            }
        });
    }

}
